package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import org.assertj.core.util.Strings;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by mz on 11/8/17.
 *
 */
@Data
public class BreedTransferRecord implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = 6200603388386327473L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 单据编号
     */
    private String number;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场名称
     */
    private String breedFactoryName;

    /**
     * 从栏舍的id
     */
    private Long fromStableId;

    /**
     * 从栏舍号
     */
    private String fromStableName;

    /**
     * 到栏舍号id
     */
    private Long toStableId;

    /**
     * 到栏舍号
     */
    private String toStableName;

    /**
     * 转群时间
     */
    private Date date;

    /**
     * 转移动物数量
     */
    private Integer quantity;

    /**
     * 耳标号json
     */
    @JsonIgnore
    private String earTagNumber;

    /**
     * 耳标号list
     */
    private List<String> earTagNumbers;

    /**
     * 状态 0、未提交 1、完成
     */
    private Integer status;

    /**
     * 记录状态：1、有效 -1、失效
     */
    private Integer deleteFlag;

    private Date createdAt;

    private Date updatedAt;


    public void setEarTagNumber(String earTagNumber){

        this.earTagNumber = earTagNumber;

        if(Strings.isNullOrEmpty(earTagNumber)){
            this.earTagNumbers = new ArrayList<>();
        }else {
            try {
                this.earTagNumbers = objectMapper.readValue(earTagNumber, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }

    public void setEarTagNumbers(List<String> earTagNumbers){

        this.earTagNumbers = earTagNumbers;

        if(earTagNumbers == null || earTagNumbers.isEmpty()) {
            this.earTagNumber = null;
        }else {
            try {
                this.earTagNumber = objectMapper.writeValueAsString(earTagNumbers);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }
}
